package cn.flyingocean.spider.impl.com.douban.book.com.douban.book;

import cn.flyingocean.spider.impl.com.douban.book.WebClientSpider;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

/**
 * 利用豆瓣图书的搜索页面来搜索图书，该页面使用了页面内动态加载
 * 即使使用了Webclient仍然有些问题
 */
public class DoubanSpider_ISBN extends WebClientSpider {

    private String url = "https://book.douban.com/subject_search?search_text=9787562070146";


    public Object doWork(Document document) {
        return null;
    }

    public Object doWork(String url) {
        return null;
    }

    /**
     * 参考链接 https://blog.csdn.net/dietime1943/article/details/79035779
     * @return
     */
    public Object doWork() {
        try {
            HtmlPage page = getWebClient().getPage(this.getUrl());
            String pageAsXml = page.asXml();
            // Jsoup解析处理
            Document document = Jsoup.parse(pageAsXml);
            // 此处省略其他操作
            System.out.println(document.toString());
            Element div_class_item_root = document.select("link[ref=canonical]").first();
            Element link_ref__canonical = div_class_item_root.children().first();
            String targetUrl = link_ref__canonical.attr("href");
            return targetUrl;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getUrl() {
        return url;
    }

    String getOutputPath() {
        return null;
    }

    public void beforeDead() {
        super.beforeDead();
    }
}
